from django.db import models


class UserInfo(models.Model):
    '''
    用户表
    '''
    username = models.CharField(verbose_name='登陆账户名称', max_length=30, null=False)
    fullname = models.CharField(verbose_name='姓名全称', max_length=50, null=False)
    password = models.CharField(verbose_name='登陆密码', max_length=50, null=False)
    create_time = models.DateTimeField(verbose_name='账户创建时间', auto_now_add=True)
    phone = models.CharField(verbose_name='手机号码', max_length=11, null=True)
    email = models.EmailField(verbose_name='邮箱', max_length=50, null=True, blank=True)
    role = models.CharField(verbose_name='角色', max_length=60, null=True)
    superior = models.CharField(verbose_name='上级', max_length=50, null=True, blank=True)
    depart_id = models.ManyToManyField(verbose_name='部门', to='Department', null=True, blank=True)

    def __str__(self):
        return self.username


class Jurisdiction(models.Model):
    '''
    权限表
    '''
    jurisdiction = models.CharField(verbose_name='权限名称', max_length=30)
    url = models.CharField(verbose_name='含正则的URL', max_length=128, null=True)
    is_menu = models.BooleanField(verbose_name='是否可做菜单', default=False)

    def __str__(self):
        return self.jurisdiction


class Department(models.Model):
    ''''
    部门表
    '''
    department_code = models.CharField(verbose_name='部门代码', max_length=50)
    department_name = models.CharField(verbose_name='部门名称', max_length=30)
    jurisdiction = models.ManyToManyField(verbose_name='拥有的所有权限', to='Jurisdiction', blank=True)

    def __str__(self):
        return self.department_name


class Product(models.Model):
    '''
    产品表
    '''
    model = models.CharField(verbose_name='型号', max_length=30, null=False)
    name = models.CharField(verbose_name='产品名称', max_length=200, unique=True, null=False)
    cpu = models.CharField(verbose_name='cpu型号', max_length=40, null=False)
    flash = models.CharField(verbose_name='flash容量', max_length=40, null=False)
    memory = models.CharField(verbose_name='内存容量', max_length=40, null=False)
    board = models.CharField(verbose_name='board型号', max_length=30, unique=True, null=False)
    country = models.CharField(verbose_name='国家码', max_length=20, default='CN')
    company = models.CharField(verbose_name='公司名称', max_length=40, default='金中熠')
    company_id = models.IntegerField(verbose_name='公司ID', null=False)
    mac_inc = models.IntegerField(verbose_name='MAC数量', default=2, null=False)
    mark = models.CharField(verbose_name='mark', max_length=10, null=False, default='C')
    mark1 = models.PositiveSmallIntegerField(verbose_name='标记', default=1, null=False)
    mqtt = models.CharField(verbose_name='cmagent_server', max_length=20, null=True)
    device_id = models.CharField(verbose_name='cmagent连接平台', max_length=32, null=True)
    device_id_pwd = models.CharField(verbose_name='cmagent连接平台密码', max_length=32, null=True)
    address = models.CharField(verbose_name='内网地址', max_length=22, null=True)
    pubkey = models.CharField(verbose_name='公key', max_length=44, null=True)
    endpoint_host = models.CharField(verbose_name='远端服务器IP', max_length=20, null=True)
    endpoint_port = models.CharField(verbose_name='远端服务器端口', max_length=10, null=True)
    future = models.CharField(verbose_name='特殊字段', max_length=44, default='0xffff', null=False)


class Mac(models.Model):
    '''
    MAC延续记录表
    '''
    company_id = models.IntegerField(verbose_name='公司ID')
    mac_curr = models.CharField(verbose_name='当前MAC', max_length=12)


class Macs(models.Model):
    '''
    存放MAC数据表
    '''
    mac = models.CharField(verbose_name='MAC地址', max_length=12, unique=True, null=True)
    uuid = models.CharField(verbose_name='uuid', max_length=50, null=True)
    model = models.CharField(verbose_name='型号model', max_length=50, null=True)
    board = models.CharField(verbose_name='型号', max_length=50, null=True)
    country = models.CharField(verbose_name='国家码', max_length=50, null=True)
    deviceid = models.CharField(verbose_name='cmagent用户名', max_length=16, null=True)
    secret = models.CharField(verbose_name='cmagent密码', max_length=32, null=True)
    mqtt = models.CharField(verbose_name='cmagent_server', max_length=32, null=True)
    private_key = models.CharField(verbose_name='私key', max_length=44, null=True)
    dutpublic_key = models.CharField(verbose_name='根据private_key生成的公key', max_length=44, null=True)
    public_key = models.CharField(verbose_name='服务器公key', max_length=44, null=True)
    address = models.CharField(verbose_name='内网IP', max_length=33, null=True)
    endpoint_host = models.CharField(verbose_name='服务器IP', max_length=20, null=True)
    endpoint_port = models.CharField(verbose_name='服务器端口', max_length=4, null=True)
    future = models.CharField(verbose_name='特殊字段', max_length=44, null=True)
    mark = models.CharField(verbose_name='mark', max_length=10, null=False, default='C')

    def __str__(self):
        return self.mac


class MacGenerateRecode(models.Model):
    '''
    存放MAC生成记录数据表
    '''
    product_name = models.CharField(verbose_name='产品名称', max_length=30, null=True)
    hardware_name = models.CharField(verbose_name='硬件名称', max_length=30, null=True)
    generate_datetime = models.DateTimeField(verbose_name='生成时间', auto_now_add=True)
    board = models.CharField(verbose_name='型号', max_length=30, null=True)
    mac_start = models.CharField(verbose_name='MAC起始', max_length=12, null=True)
    mac_end = models.CharField(verbose_name='MAC结束', max_length=12, null=True)
    mac_nums = models.PositiveIntegerField(verbose_name='生成的MAC数量', default=1, null=True)
    id_identification = models.CharField(verbose_name='id区间段', max_length=10, null=True)
    user_id = models.ForeignKey(verbose_name='用户id', to='UserInfo', default='', on_delete=models.CASCADE)

    def __str__(self):
        return self.hardware_name


class Notice(models.Model):
    notice_number = models.CharField(verbose_name='通知单号', max_length=64, null=False)
    consignee = models.CharField(verbose_name='收货人', max_length=30, null=False)
    delivery_department = models.ForeignKey(verbose_name='发货部门', to='Department', default='', on_delete=models.CASCADE)
    delivery_date = models.DateTimeField(verbose_name='发货日期', auto_now_add=True)
    phone = models.CharField(verbose_name='客户电话', max_length=11, null=False)
    address = models.CharField(verbose_name='客户地址', max_length=128, null=False)
    SOURCE_CHOICES = [
        (1, '线下'),
        (2, '淘宝'),
        (3, '京东'),
        (4, '代理商')
    ]
    source = models.IntegerField(verbose_name='来源', choices=SOURCE_CHOICES)
    nums = models.IntegerField(verbose_name='出货数量', null=False)
    finish_nums = models.IntegerField(verbose_name='已完成数量', default=0, null=False)
    mac_address = models.TextField(verbose_name='MAC地址', null=False)
    remarks = models.TextField(verbose_name='备注', max_length=1000, blank=True, null=True)
    EXAMINE_CHOICES = [
        (0, '未审核'),
        (1, '未通过'),
        (2, '通过'),
    ]
    STATUS_CHOICES = [
        (0, '待处理'),
        (1, '备货中'),
    ]
    examine = models.IntegerField(verbose_name='审核结果', choices=EXAMINE_CHOICES, default=0, null=False)
    status = models.IntegerField(verbose_name='备货状态', choices=STATUS_CHOICES, default=0, null=False)
    user = models.ForeignKey(verbose_name='用户', to='UserInfo', default='', on_delete=models.CASCADE)


class ExamineDesc(models.Model):
    examine_desc = models.TextField(verbose_name='审核未能过原因描述', null=False)
    delivery_order_id = models.IntegerField(verbose_name='出货单ID', null=False)
